package com.example.tvfoucssustom;

import android.os.Bundle;
import android.support.v4.app.Fragment;
import android.support.v4.content.ContextCompat;
import android.support.v7.app.AppCompatActivity;
import android.view.View;

import com.example.tvfoucssustom.base.PagerFragmentAdapter;
import com.example.tvfoucssustom.custom.TvTabLayout;
import com.example.tvfoucssustom.fragment.MessageFragment;
import com.example.tvfoucssustom.fragment.MusicFragment;
import com.example.tvfoucssustom.fragment.PhotoFragment;
import com.example.tvfoucssustom.fragment.SettingFragment;
import com.example.tvfoucssustom.fragment.VideoFragment;
import com.example.tvfoucssustom.fragment.WoxiuFragment;
import com.example.tvfoucssustom.util.DebugLog;
import com.example.tvfoucssustom.view.NoScrollViewPager;

import java.util.ArrayList;
import java.util.List;

import static com.example.tvfoucssustom.util.DebugLog.LOG;

public class HomeTwoctivity extends AppCompatActivity {
    private static final String TAG = HomeTwoctivity.class.getSimpleName();
    private TvTabLayout mTabLayout;
    private List<String> list;
//        private ViewPager viewPager;
    private NoScrollViewPager viewPager;
    private List<Fragment> fragments;
    private PagerFragmentAdapter pagerAdapter;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_home_two);
        mTabLayout = findViewById(R.id.tablayout);
        viewPager = findViewById(R.id.view_page);
        viewPager.setScanScroll(false);
        iniData();
        ininView();
        initEvent();
    }

    private void initEvent() {
        mTabLayout.addOnTabSelectedListener(new TvTabLayout.OnTabSelectedListener() {
            @Override
            public void onTabSelected(TvTabLayout.Tab tab) {
                DebugLog.d(LOG + TAG + " ---- onTabSelected " + tab.getPosition());
                viewPager.setCurrentItem(tab.getPosition());

            }

            @Override
            public void onTabUnselected(TvTabLayout.Tab tab) {
                DebugLog.d(LOG + TAG + " ---- onTabUnselected " + tab.getPosition());
//                mTabLayout.setIndicatorDrawable(ContextCompat.getDrawable(Homectivity.this, R.color.transparent));
            }

            @Override
            public void onTabReselected(TvTabLayout.Tab tab) {
                DebugLog.d(LOG + TAG + " ---- onTabReselected " + tab.getPosition());
            }
        });
        mTabLayout.setOnFocusChangeListener(new View.OnFocusChangeListener() {
            @Override
            public void onFocusChange(View v, boolean hasFocus) {
                if (hasFocus) {
                    mTabLayout.setIndicatorDrawable(ContextCompat.getDrawable(HomeTwoctivity.this, R.mipmap.zx_p1_light));
                } else {
                    mTabLayout.setIndicatorDrawable(ContextCompat.getDrawable(HomeTwoctivity.this, R.color.transparent));
                }
            }
        });
//        tabLayoutOnPageChangeListener
//        viewPager.addOnPageChangeListener(new ViewPager.OnPageChangeListener() {
//            @Override
//            public void onPageScrolled(int i, float v, int i1) {
//
//            }
//
//            @Override
//            public void onPageSelected(int i) {
//                DebugLog.d(LOG + TAG + " ---- onPageSelected---- " + i);
//                mTabLayout.setSelectedTabView(i);
//
//
//            }
//
//            @Override
//            public void onPageScrollStateChanged(int i) {
//
//            }
//        });
    }

    private void iniData() {
        list = new ArrayList<>();
        list.add("我秀沃家");
        list.add("相册");
        list.add("音乐");
        list.add("影视");
        list.add("消息");
        list.add("设置");

        fragments = new ArrayList<>();
        fragments.add(new WoxiuFragment());
        fragments.add(new PhotoFragment());
        fragments.add(new MusicFragment());
        fragments.add(new VideoFragment());
        fragments.add(new MessageFragment());
        fragments.add(new SettingFragment());

        pagerAdapter = new PagerFragmentAdapter(getSupportFragmentManager(), fragments, list);

    }

    private void ininView() {
        // 代码动态添加tab
        for (int i = 0; i < list.size(); i++) {
            mTabLayout.addTab(mTabLayout.newTab().setText(list.get(i)), i == 0);
        }
        viewPager.setAdapter(pagerAdapter);
//        mTabLayout.setTabsFromPagerAdapter(pagerAdapter);
        //这个是可以viewpager滑动的，上面指示器也可以滑动
//        mTabLayout.setupWithViewPager(viewPager, false);
//        viewPager.setCurrentItem(0);
    }

}
